<template>
    <el-dialog :title="dialogStatus | titleFilter" :close-on-click-modal="false" width="600px" :visible.sync="visible">
        <el-form ref="temp" :model="temp" :rules="rule" label-width="120px" @keyup.enter.native="dataFormSubmit()">

            <el-form-item label="园区名称" prop="park_name" v-if="temp.type == 1 && dialogStatus == 'create'">
                <!-- <el-select v-model="temp.park_id" placeholder="请选择" @change="park_change">
                    <el-option v-for="item in park_options" :key="item.id" :label="item.name" :value="{'id':item.id,'park_id':item.parkId}">
                    </el-option>
                </el-select> -->
                <el-input v-model="temp.park_name" placeholder="请输入" disabled />
            </el-form-item>

            <el-form-item :label="temp.type-1 | lableFilter" prop="name">
                <el-input v-model="temp.name" placeholder="请输入" />
            </el-form-item>

            <!-- <el-form-item label="备注" prop="remark">
                <el-input v-model="temp.remark" placeholder="备注" />
            </el-form-item> -->

        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">
                取消
            </el-button>
            <el-button type="primary" @click="dataFormSubmit">
                确认
            </el-button>
        </span>
    </el-dialog>
</template>
<script>
import { getParkListAll, createHouseTree, updateHouseTree } from '@/api/assets'// 请求数据
var ctrl_type = {
    add_building: 1,
    up_building: 1,
    delete_building: 1,
    add_unit: 2,
    up_unit: 2,
    delete_unit: 2,
    add_floor: 3,
    up_floor: 3,
    delete_floor: 3
}
export default {
    filters: {
        // 过滤弹框的标题
        titleFilter (type) {
            const textMap = {
                create: '新增',
                update: '修改'
            }
            return textMap[type]
        },
        //新增属性名
        // 过滤弹框的标题
        lableFilter (type) {
            const textMap = ['楼栋', '单元', '楼层']
            return textMap[type]
        }
    },
    data () {
        return {
            visible: false,
            dialogStatus: '', // 弹框新增或者修改
            park_options: [], // 园区list
            temp: { // 表单数据
                id: '',
                park_name: '', // 园区名称
                park_id: '',
                name: '', // 楼栋、单元、楼层 名称
                parent_id: '', // 父级id
                type: 1// 新增类型
                // remark: ''
            },
            rule: {
                name: [
                    { required: true, message: '不能为空', trigger: 'blur' }
                ],
                park_name: [
                    { required: true, message: '不能为空', trigger: 'blur' }
                ]
            }
        }
    },
    created () {
    },
    methods: {
        // 初始化
        init (status, param) {
            //   console.log(param)
            this.dialogStatus = status // 标题
            this.visible = true // 显示
            this[status](param) // 新增或者修改
            this.$nextTick(() => { // 重置表单
                this.$refs['temp'].clearValidate()
            })
        },
        create (param) {
            this.resetTemp(param) // 清空表单
            // this.get_park()
        },
        async update (param) {
            console.log(param)
            let data = Object.assign({}, param)//从上一页带过来当前的数据
            //   const { data, code } = await getParkDetails({}, param.id) // 从后台获取当前的的数据
            // 成功
            this.temp.id = data.id
            this.temp.park_name = data.parkName // 园区名称
            // this.temp.park_id = data.a
            this.temp.name = data.storeyName // 楼栋、单元、楼层 名称
            // this.temp.parent_id = data.a // 父级id
            // this.temp.type = data.a// 新增类型
        },
        // 获取园区
        async get_park () {
            // 请求
            const res = await getParkListAll().catch(e => { })
            // 失败
            if (!res) { return }
            // 成功
            const { data, code, message } = res
            this.park_options = data
            // console.log(data)
        },
        // 新增数据清空表单数据
        resetTemp (param) {
            // add_building>新增楼栋
            // up_building">修改楼栋
            // delete_building>删除楼栋
            // add_unit">新增单元
            // up_unit">修改单元
            // delete_unit">删除单元
            // add_floor">新增楼层
            // up_floor">修改楼层
            // delete_floor">删除楼层

            this.temp = {
                id: '',
                park_name: param.storeyName, // 园区名称
                park_id: param.parkId,
                name: '', // 楼栋、单元、楼层 名称
                parent_id: param.id, // 父级id
                // type: param.type + 1// 新增类型
                type: ctrl_type[param.addtype]
            }
        },
        // 表单提交
        dataFormSubmit () {
            this.$refs['temp'].validate(async (valid) => {
                if (!valid) return
                const params = {
                    // 'buidingId': this.temp.id || undefined,
                    // 'name': this.temp.name,
                    'parkId': this.temp.park_id,
                    'type': this.temp.type,
                    'parentId': this.temp.parent_id, //
                    'storeyName': this.temp.name
                }
                const up_params = {
                    'id': this.temp.id || undefined,
                    'storeyName': this.temp.name
                }
                const textMap = {
                    create: createHouseTree, // 新增
                    update: updateHouseTree // 修改
                }

                // 提交表单
                const { code, message } = await textMap[this.dialogStatus](this.dialogStatus == 'create' ? params : up_params)

                // 成功
                this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                        this.visible = false
                        this.$emit('getList')
                        this.$emit('getTree')
                    }
                })
            })
        },

        // ===>>>>>>>>>

        // 选择园区
        park_change (e) {
            console.log(e)
        }

    }
}
</script>

<style lang="scss">
.mod-menu {
    .menu-list__input,
    .icon-list__input {
        > .el-input__inner {
            cursor: pointer;
        }
    }
    &__icon-popover {
        width: 458px;
        overflow: hidden;
    }
    &__icon-inner {
        width: 478px;
        max-height: 258px;
        overflow-x: hidden;
        overflow-y: auto;
    }
    &__icon-list {
        width: 458px;
        padding: 0;
        margin: -8px 0 0 -8px;
        > .el-button {
            padding: 8px;
            margin: 8px 0 0 8px;
            > span {
                display: inline-block;
                vertical-align: middle;
                width: 18px;
                height: 18px;
                font-size: 18px;
            }
        }
    }
    .icon-list__tips {
        font-size: 18px;
        text-align: center;
        color: #e6a23c;
        cursor: pointer;
    }
}
</style>
